import { Component, OnInit } from '@angular/core';
import {HttpService} from '../http.service';
import {ColumnItem, Course, CourseGPA, CourseInfo, Student} from '../student';

@Component({
  selector: 'app-course-gpa',
  templateUrl: './course-gpa.component.html',
  styleUrls: ['./course-gpa.component.css']
})
export class CourseGPAComponent implements OnInit {
  courseinfo: Course[] = [];
  private courseinfoData: Course[];
  courseData: CourseInfo[] = [];
  private courseDataPlay: CourseInfo[];
  courseGPA: CourseGPA[] = [];
  private sumGPA: number;
  loading = true;
  listOfColumns: ColumnItem[] = [
    {
      name: '课程编号',
    },
    {
      name: '课程名称',
    },
    {
      name: '课程类型',
      filterMultiple: false,
      listOfFilter: [
        { text: '选修课', value: '选修课' },
        { text: '必修课', value: '必修课' },
      ],
      filterFn: (type: string, item: CourseGPA) => item.type.indexOf(type) !== -1
    },
    {
      name: '课程学分',
    },
    {
      name: '选课人数',
      sortOrder: null,
      sortFn: (a: CourseGPA, b: CourseGPA) => a.peopleNumber - b.peopleNumber,
    },
    {
      name: '平均绩点',
      sortOrder: 'descend',
      sortFn: (a: CourseGPA, b: CourseGPA) => a.AveGPA - b.AveGPA,
    }
  ];
  private peopleNumber: number;
  constructor(private httpService: HttpService) { }
  getCourseGPA() {
    this.httpService.course_select1().subscribe(
      res => {
        res.data.forEach(cou => {
          this.courseinfo = [...this.courseinfo];
          this.courseinfo.push({
            courseid: cou.courseid,
            score: cou.score,
            stuid: cou.stuid,
          });
        });
        console.log(this.courseinfo);
        this.courseinfoData = this.courseinfo;
      });
    this.httpService.getCourses().subscribe(
      res => {
        res.data.forEach(cou => {
          this.courseData = [...this.courseData];
          this.courseData.push({
            courseid: cou.courseid,
            coursename: cou.coursename,
            credit: cou.credit,
            type: cou.type
          });
        });
        console.log(this.courseData);
        this.courseDataPlay = this.courseData;
      });
    this.httpService.getCourses().subscribe(
      res => {
        res.data.forEach(cou => {
          this.courseGPA = [...this.courseGPA];
          this.courseGPA.push({
            AveGPA: this.getGPA(cou.courseid),
            peopleNumber: this.getPeopleNumber(cou.courseid),
            courseid: cou.courseid,
            coursename: cou.coursename,
            credit: cou.credit,
            type: cou.type
          });
        });
        console.log(this.courseGPA);
        this.loading = false
      });
  }
  getGPA(courseid) {
    this.sumGPA = 0;
    this.peopleNumber = 0;
    for (const i in this.courseinfoData) {
      if (courseid === this.courseinfo[i].courseid) {
        this.sumGPA += this.courseinfoData[i].score / 10 - 5;
        this.peopleNumber += 1;
      }
    }
    return this.sumGPA / this.peopleNumber;
  }
  getPeopleNumber(courseid) {
    this.peopleNumber = 0;
    for (const i in this.courseinfoData) {
      if (courseid === this.courseinfo[i].courseid) {
        this.peopleNumber += 1;
      }
    }
    return this.peopleNumber;
  }
  ngOnInit(): void {
    this.getCourseGPA();
  }
}
